//
//  GameController.swift
//  appstore
//
//  Created by 王雷 on 2019/11/2.
//  Copyright © 2019 Roy. All rights reserved.
//

import UIKit

class GameController: BaseViewController,UITableViewDelegate,UITableViewDataSource {
    
    let GameRecommandCellIdentifier = "GameRecommandCellIdentifier"
    let GameTopicCellIdentifier = "GameTopicCellIdentifier"
    let QuickTitleCellIdentifier = "QuickTitleCellIdentifier"
    
    var gameTableView:UITableView!
    
    let data: [String] = [
        "Add a Payment Method",
        "Parents' Guide to the App",
        "About In-App Purchases",
        "About Apps & Games for Your Kids",
        "About Personalisation",
        "New to the App Store",
    ]

    override func viewDidLoad() {
        super.viewDidLoad()

        self.initView()
    }
    
    func initView() -> Void {
        self.navigationItem.title = "Game";
        self.navigationController?.navigationBar.prefersLargeTitles = true
        
        gameTableView = UITableView(frame: CGRect.zero, style: .plain)
        gameTableView.delegate = self
        gameTableView.dataSource = self
        gameTableView.separatorInset = UIEdgeInsets(top: 0.0, left: 20.0, bottom: 0.0, right: 20.0)
        self.view.addSubview(gameTableView)
        gameTableView.snp.makeConstraints { (make) in
            make.left.top.right.bottom.equalTo(0.0)
        }
        gameTableView.register(GameRecommandCell.self, forCellReuseIdentifier: GameRecommandCellIdentifier)
        gameTableView.register(TopicCell.self, forCellReuseIdentifier: GameTopicCellIdentifier)
        gameTableView.register(LinkCell.self, forCellReuseIdentifier: QuickTitleCellIdentifier)
    
    }
    
    // UITableViewDataSource
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count + 3
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: GameRecommandCellIdentifier) as! GameRecommandCell
            cell.selectItemIndex = {[weak self](index) in
                self!.targetDetailController()
            }
            return cell
        } else if indexPath.row == 1 {
            let cell = tableView.dequeueReusableCell(withIdentifier: GameTopicCellIdentifier)
            return cell!
        } else if indexPath.row == 2 {
            let cell = tableView.dequeueReusableCell(withIdentifier: QuickTitleCellIdentifier) as! LinkCell
            cell.contentLabel.text = "Quicks Links"
            cell.separatorInset = UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: ScreenWidth)
            return cell
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: QuickTitleCellIdentifier) as! LinkCell
            cell.contentLabel.text = "\(data[indexPath.row - 3])"
            cell.contentLabel.textColor = .blue
            return cell
        }
        
    }
    
    // UITableViewDelegate
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.row == 0 {
            return 336.0
        } else if indexPath.row == 1 {
            return 282.0
        } else {
            return 45.0
        }
    }
    
    // Private Method
    func targetDetailController() -> Void {
        let gameSB = UIStoryboard(name: "Game", bundle: Bundle.main)
        let gameDetailVC = gameSB.instantiateViewController(identifier: "GameDetailController")
        gameDetailVC.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(gameDetailVC, animated: true)
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
